/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.sdk;

import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.osgi.framework.BundleContext;
import org.zoundry.gaedt.core.extensions.ZGaeExtensionLoader;
import org.zoundry.gaedt.core.util.ZGaeUtil;

/**
 * The activator class controls the plug-in life cycle
 */
public class ZGaeSDKActivator extends Plugin
{
    // The plug-in ID
    public static final String PLUGIN_ID = "org.zoundry.gaedt.sdk";
    // The shared instance
    private static ZGaeSDKActivator sPlugin;

    /**
     * @return the plugin
     */
    private static ZGaeSDKActivator getPlugin()
    {
        return sPlugin;
    }

    /**
     * @param aPlugin the plugin to set
     */
    private static void setPlugin(ZGaeSDKActivator aPlugin)
    {
        sPlugin = aPlugin;
    }

    /**
     * The constructor
     */
    public ZGaeSDKActivator()
    {
    }

    /**
     * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext aContext) throws Exception
    {
        super.start(aContext);
        setPlugin(this);

        processExtensions();
        updateSDK();
    }

    /**
     * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext aContext) throws Exception
    {
        setPlugin(null);
        super.stop(aContext);
    }

    /**
     * Reads all of the extensions contributed to us via our custom
     * extension points.
     */
    private void processExtensions()
    {
        List<IZGaeSDKObserver> contributedObservers = ZGaeExtensionLoader.discoverContributors(
                IZGaeSDKObserver.EXTENSION_POINT_ID,
                IZGaeSDKObserver.class);
        for (IZGaeSDKObserver observer : contributedObservers)
        {
            ZGaeSDK.getInstance().addObserver(observer);
        }
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static ZGaeSDKActivator getDefault()
    {
        return getPlugin();
    }

    /**
     * Updates the Google App Engine SDK, if necessary.
     */
    public void updateSDK()
    {
        Job job = new Job("Updated the Google App Engine SDK")
        {
            /**
             * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
             */
            @Override
            protected IStatus run(IProgressMonitor aMonitor)
            {
                ZGaeSDKUpdater updater = new ZGaeSDKUpdater();
                try
                {
                    updater.update(aMonitor);
                }
                catch (Exception e)
                {
                    ZGaeUtil.logAndDisplayError(e);
                }
                return Status.OK_STATUS;
            }
        };
        job.setPriority(Job.LONG);
        ISchedulingRule modifyRule = ZGaeSDKSchedulingRule.RULE;
        job.setRule(modifyRule);
        job.schedule();
    }
}
